//
//  ShareViewController.m
//  TextApplication
//
//  Created by dllo on 16/8/27.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "ShareViewController.h"
#import "TAMacro.h"
#import "ShareCollectionViewCell.h"
#import "TextView.h"
#import "ZWYNetTool.h"
#import "GoodsModel.h" // 公用的model
#import "RecommendCollectionViewCell.h" // 公用的cell
#import "UIImageView+WebCache.h"
#import "GoodsViewController.h"
#import "LikeDetailViewController.h"

#import <AVFoundation/AVFoundation.h> // 其中有图片处理的函数
//#import <ImageIO/ImageIO.h> // 导入框架

#import "MJRefresh.h"
#import "MBProgressHUD.h"

#import "NetImageSize.h"
#import "UIImageView+WebCache.h"
#import <AVFoundation/AVFoundation.h>

@interface ShareViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UITableViewDataSource, UITableViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray <GoodsModel *>*dataArray;
@property (nonatomic, strong) NSMutableArray *picArray;

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIScrollView *backScrollView;
@property (nonatomic, strong) NSArray *tableArray;

@property (nonatomic, strong) UIButton *right;
@property (nonatomic, strong) UIButton *left;
@property (nonatomic, strong) UIButton *secRight;

@property (nonatomic, copy) NSString *str3;

@property (nonatomic, assign) CGFloat itemWidth;
@property (nonatomic, assign) CGFloat itemHeight;

@property (nonatomic, strong) NSMutableArray *widthArray;
@property (nonatomic, strong) NSMutableArray *heightArray;

@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat height;

@property (nonatomic, assign) NSInteger page;
@property (nonatomic, copy) NSString *str4;
@property (nonatomic, copy) NSString *reuse;

@property (nonatomic, strong) NSMutableArray *imagesArray;
@end

@implementation ShareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor greenColor];
    self.page = 1;
    [self createTop];
    [self createCollectionView];
    
    [self createTableView];
    [self createData];
}

- (void)createTableView {
    self.backScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 0)]; // 设置滚动范围大于scrollView大小就可以滚动
    self.backScrollView.delegate = self;
    self.backScrollView.contentSize = CGSizeMake(0, HEIGHT - 100);
    self.backScrollView.backgroundColor = [UIColor blackColor];
    self.backScrollView.alpha = 0.8;
    [self.view addSubview:self.backScrollView];
    self.tableArray = [NSArray array];
    self.tableArray = @[@"全部分享", @"商店分享"];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 100) style:UITableViewStylePlain];
    self.tableView.scrollEnabled = NO;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 50;
    [self.backScrollView addSubview:self.tableView];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tableReuse"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableReuse"];
    cell.backgroundColor = [UIColor whiteColor];
    cell.textLabel.text = self.tableArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        self.page = 1;
        self.str4 = @"";
        [self createData];
    } else if (indexPath.row == 1) {
        self.page = 1;
        NSString *str1 = @"http://mobile.iliangcang.com/goods/goodsShare?a=b&coverId=1&page=";
        NSString *str2 = @"&count=12&self_host=1&app_key=iPhone&v=3.0.0&sig=254F2CCC-7E43-4833-A27D-7D12702F5981";
        self.str4 = [[str1 stringByAppendingString:[NSString stringWithFormat:@"%ld", self.page]] stringByAppendingString:str2];
        [self createData];
    }
    [UIView animateWithDuration:1 animations:^{
        self.backScrollView.frame = CGRectMake(0, 0, WIDTH, 0);
        [self.view addSubview:self.backScrollView];
    }];
    
    self.right = [TextView createButtonWithImage:[UIImage imageNamed:@"21"]];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.right];
    [self.right addTarget:self action:@selector(rightClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (self.collectionView.contentOffset.y < 0) {
        self.page = 1;
//        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//        hud.label.text = NSLocalizedString(@"loading", @"HUD loading title");
//        NSLog(@"下拉刷新");
//        [hud hideAnimated:YES afterDelay:1];
        [self createData];
    }
}

// 上拉加载
- (void)upLoad {
    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self createData];
        [self.collectionView.mj_footer endRefreshing];
    }];
}

- (void)createData {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = NSLocalizedString(@"loading", @"HUD loading title");

//    NSString *urlStr = @"http://mobile.iliangcang.com/goods/goodsShare?a=b&coverId=1&type=0&page=1&count=12&app_key=iPhone&v=3.0.0&sig=254F2CCC-7E43-4833-A27D-7D12702F5981&user_token=03723b5fa4100c1079e788f94d65908d";
    NSDictionary *headDic = @{@"Host": @"mobile.iliangcang.com", @"Proxy-Connection": @"keep-alive", @"Accept": @"*/*", @"Accept-Language": @"zh-Hans-CN;q=1, en-CN;q=0.9", @"Connection": @"keep-alive", @"User-Agent": @"iLiangCang/3.0.0 (iPhone; iOS 9.2.1; Scale/2.00) Paros/3.2.13"};
    
//    NSString *stringURl = [NSString stringWithFormat:@"http://mobile.iliangcang.com/goods/goodsShare?a=b&coverId=1&page=%ld&count=12&self_host=1&app_key=iPhone&v=3.0.0&sig=254F2CCC-7E43-4833-A27D-7D12702F5981",self.page];
    
    if (self.str4.length == 0) {
        
        NSString *str1 = @"http://mobile.iliangcang.com/goods/goodsShare?a=b&coverId=1&type=0&page=";
        NSString *str2 = @"&count=12&app_key=iPhone&v=3.0.0&sig=254F2CCC-7E43-4833-A27D-7D12702F5981&user_token=03723b5fa4100c1079e788f94d65908d";
        self.str3 = [[str1 stringByAppendingString:[NSString stringWithFormat:@"%ld", self.page]] stringByAppendingString:str2];
       
    } else {
        NSString *str1 = @"http://mobile.iliangcang.com/goods/goodsShare?a=b&coverId=1&page=";
        NSString *str2 = @"&count=12&self_host=1&app_key=iPhone&v=3.0.0&sig=254F2CCC-7E43-4833-A27D-7D12702F5981";
        self.str4 = [[str1 stringByAppendingString:[NSString stringWithFormat:@"%ld", self.page]] stringByAppendingString:str2];
        self.str3 = self.str4;
    }
    [ZWYNetTool GET:self.str3 andBody:nil andHeader:headDic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        if (self.page == 1) {
            self.dataArray = [NSMutableArray array];
            self.imagesArray = [NSMutableArray array];
            self.widthArray = [NSMutableArray array];
            self.heightArray = [NSMutableArray array];
        }
        NSArray *array = result[@"data"][@"items"];
        for (NSDictionary *dic in array) {
            GoodsModel *good = [GoodsModel modelWithDic:dic];
            [self.dataArray addObject:good];
        }
        if (self.dataArray.count != 0) {
            [hud hideAnimated:YES afterDelay:0.5];
        }
        self.page = self.page + 1;
        [self.collectionView reloadData];
        } andFailureBlock:^(NSError *error) {
    }];
}


- (void)createTop {
    self.title = @"分享";
    self.view.backgroundColor = [UIColor yellowColor];
//    self.left = [TextView createButtonWithImage:[UIImage imageNamed:@"19"]];
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.left];
    
    self.right = [TextView createButtonWithImage:[UIImage imageNamed:@"21"]];
    [self.right addTarget:self action:@selector(rightClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.right];
}

- (void)rightClick:(UIButton *)button {
    [UIView animateWithDuration:1 animations:^{
        self.backScrollView.frame = CGRectMake(0, 0, WIDTH, HEIGHT );
        [self.view addSubview:self.backScrollView];
    }];
    self.secRight = [TextView createButtonWithImage:[UIImage imageNamed:@"6"]];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.secRight];
    [self.secRight addTarget:self action:@selector(secRightClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)secRightClick:(UIButton *)button {
    
    [UIView animateWithDuration:1 animations:^{
        self.backScrollView.frame = CGRectMake(0, 0, WIDTH, 0);
        [self.view addSubview:self.backScrollView];
    }];
    self.right = [TextView createButtonWithImage:[UIImage imageNamed:@"21"]];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.right];
    [self.right addTarget:self action:@selector(rightClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.itemWidth = WIDTH / 2 - 15;
    self.itemHeight = WIDTH / 2 - 15;
    layout.itemSize = CGSizeMake(self.itemWidth, self.itemHeight);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 110) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[ShareCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    [self upLoad];
//    self.reuse = [@"reuse" stringByAppendingString:[NSString stringWithFormat:@"%ld", indexPath.row]];
//    ShareCollectionViewCell *cell = (ShareCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
//    if (!cell) {
//        cell = [[ShareCollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 172.5, 172.5)];
//    }
    
    ShareCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
//    cell.urlStr = self.dataArray[indexPath.row].goods_image;
    cell.goodsM = self.dataArray[indexPath.row];
//    cell.picWidth = [self.widthArray[indexPath.row] floatValue];
//    cell.picHeight = [self.heightArray[indexPath.row] floatValue];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.str4.length ==0) {
        GoodsViewController *goodVC = [[GoodsViewController alloc] init];
        goodVC.good_id = [self.dataArray[indexPath.row] goods_id];
        [self.navigationController pushViewController:goodVC animated:YES];
    } else {
        NSLog(@"%@", [self.dataArray[indexPath.row]  goods_id]);
        LikeDetailViewController *detailVC = [[LikeDetailViewController alloc] init];
        detailVC.goodID = [self.dataArray[indexPath.row]  goods_id];
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
